package com.mimc.controller.admin;

import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;
import com.mimc.core.model.SysUploadFile;
import com.mimc.core.model.SysUploadGroup;
import com.mimc.core.service.SysUploadFileService;
import com.mimc.core.service.SysUploadGroupService;
import com.mimc.framework.domain.AjaxResult;
import com.mimc.service.ShUploadService;
import io.jboot.db.model.Columns;
import io.jboot.utils.StrUtil;
import io.jboot.web.controller.JbootController;
import io.jboot.web.controller.annotation.RequestMapping;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;

@Slf4j
@RequestMapping("/admin/sys/upload")
public class SysUploadController extends JbootController {
    @Inject
    private ShUploadService shUploadService;
    @Inject
    private SysUploadGroupService sysUploadGroupService;
    @Inject
    private SysUploadFileService sysUploadFileService;
    public AjaxResult image() throws Exception {
        UploadFile file=getFile("file");//这个必须其他参数前面 不然后面的参数为null
        Integer groupId=getInt("groupId",0);
        String url=shUploadService.uploadImage(file,groupId);
        if(url!=null){
            return AjaxResult.success("",url);
        }else{
            return AjaxResult.error("上传失败");
        }

    }
    //上传分组
    public AjaxResult category(){
        List<SysUploadGroup> list=sysUploadGroupService.findListByColumns(Columns.create("is_delete",0),"sort asc");
        return AjaxResult.success("",list);
    }
    public AjaxResult addCategory(){
        String groupName=getPara("groupName");
        String groupType=getPara("groupType","image");
        Integer sort=getInt("sort",0);
        SysUploadGroup sysUploadGroup=new SysUploadGroup();
        sysUploadGroup.setGroupName(groupName);
        sysUploadGroup.setGroupType(groupType);
        sysUploadGroup.setSort(sort);
        sysUploadGroup.setCreateTime(new Date());
        sysUploadGroup.setUpdateTime(new Date());
        sysUploadGroupService.save(sysUploadGroup);
        return AjaxResult.success("");
    }
    public AjaxResult updateCategory(){
        String groupName=getPara("groupName");
        String groupType=getPara("groupType","image");
        Integer sort=getInt("sort",0);
        Integer groupId=getInt("groupId");
        SysUploadGroup sysUploadGroup=sysUploadGroupService.findById(groupId);
        if(sysUploadGroup!=null){
            if(StrUtil.isNotEmpty(groupName)){
                sysUploadGroup.setGroupName(groupName);
            }
            if(StrUtil.isNotEmpty(groupType)){
                sysUploadGroup.setGroupType(groupType);
            }
            if(sort>0){
                sysUploadGroup.setSort(sort);
            }
            sysUploadGroup.setUpdateTime(new Date());
            sysUploadGroupService.update(sysUploadGroup);
            return AjaxResult.success("");
        }else{
            return AjaxResult.error("该分组不存在");
        }
    }
    public AjaxResult deleteCategory(){
        Integer groupId=getInt("groupId");
        SysUploadGroup sysUploadGroup=sysUploadGroupService.findById(groupId);
        if(sysUploadGroup!=null){
            sysUploadGroup.setIsDelete(true);
            sysUploadGroupService.update(sysUploadGroup);
            return AjaxResult.success("");
        }else{
            return AjaxResult.error("该分组不存在");
        }
    }
    public AjaxResult fileList(){
        int page=getInt("page",1);
        int pageSize=getInt("pageSize",36);
        int groupId=getInt("groupId",0);
        Columns columns=Columns.create("is_delete",0);
        if(groupId>0){
            columns.eq("group_id",groupId);
        }
        Page<SysUploadFile> list=sysUploadFileService.paginateByColumns(page,pageSize,columns,"update_time desc");
        return AjaxResult.success("",list);
    }
    public AjaxResult deleteFile(){
        Integer fileId=getInt("fileId");
        SysUploadFile uploadFile=sysUploadFileService.findById(fileId);
        if(uploadFile==null){
            return AjaxResult.error("该文件不存在");
        }
        uploadFile.setIsDelete(true);
        uploadFile.setUpdateTime(new Date());
        sysUploadFileService.update(uploadFile);
        return AjaxResult.error("删除成功");
    }
    public AjaxResult deleteFiles(){
        String fileIds=getPara("fileIds");
        Columns columns=Columns.create();
        columns.in("file_id",fileIds.split(","));
        List<SysUploadFile> list=sysUploadFileService.findListByColumns(columns);
        for (SysUploadFile uploadFile:list){
            uploadFile.setIsDelete(true);
            uploadFile.setUpdateTime(new Date());
            sysUploadFileService.update(uploadFile);
        }
        return AjaxResult.success("删除成功");
    }
    public AjaxResult moveFiles(){
        String fileIds=getPara("fileIds");
        Integer groupId=getInt("groupId",0);
        Columns columns=Columns.create();
        columns.in("file_id",fileIds.split(","));
        List<SysUploadFile> list=sysUploadFileService.findListByColumns(columns);
        for (SysUploadFile uploadFile:list){
            uploadFile.setGroupId(groupId);
            uploadFile.setUpdateTime(new Date());
            sysUploadFileService.update(uploadFile);
        }
        return AjaxResult.success("删除成功");
    }
}
